<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------

namespace plugins\auto_code;

use cmf\lib\Plugin;

class AutoCodePlugin extends Plugin
{
    public $info = [
        'name' => 'AutoCode', //Demo插件英文名，改成你的插件英文就行了
        'title' => '代码生成器',
        'description' => '代码生成器',
        'status' => 1,
        'author' => 'Li',
        'version' => '1.0',
        'demo_url' => 'http://demo.thinkcmf.com',
        'author_url' => 'http://www.thinkcmf.com',
    ];

    public $hasAdmin = 1; //插件是否有后台管理界面

    // 插件安装
    public function install()
    {
        $adminPath = app()->getRootPath() . 'vendor/thinkcmf/cmf-app/src/src/admin/';
        $publicPath = app()->getRootPath() . 'public/';
        $pluginPath = $publicPath . 'plugins/auto_code/';
        $adminJsPath = $pluginPath . 'extra/admin.js';
        $filelogoPath = $pluginPath . 'extra/autocode_filelogo.png';
        $videologoPath = $pluginPath . 'extra/autocode_videologo.png';

        $distAdminJsPath = $publicPath . 'static/js/admin.js';
        $distFilelogoPath = $publicPath . 'static/images/autocode_filelogo.png';
        $distVideologoPath = $publicPath . 'static/images/autocode_videologo.png';

        //判断controller 和model 目录是否存在
        if (!is_dir($adminPath . 'controller/')) {
            mkdir($adminPath . 'controller/', 0777, true);
        }

        if (!is_dir($adminPath . 'model/')) {
            mkdir($adminPath . 'model/', 0777, true);
        }

        file_put_contents($distAdminJsPath, file_get_contents($adminJsPath));
        file_put_contents($distFilelogoPath, file_get_contents($filelogoPath));
        file_put_contents($distVideologoPath, file_get_contents($videologoPath));

        return true; //安装成功返回true，失败false
    }

    // 插件卸载
    public function uninstall()
    {
        $publicPath = app()->getRootPath() . 'public/';
        $distFilelogoPath = $publicPath . 'static/images/autocode_filelogo.png';
        $distVideologoPath = $publicPath . 'static/images/autocode_videologo.png';

        @unlink($distFilelogoPath);
        @unlink($distVideologoPath);

        return true; //卸载成功返回true，失败false
    }
}
